package com.jeesite.modules.purchaseoutbound.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.purchaseoutbound.entity.PurchaseOutboundHeader;
import com.jeesite.modules.purchaseoutbound.dao.PurchaseOutboundHeaderDao;
import com.jeesite.modules.purchaseoutbound.entity.PurchaseOutboundLines;
import com.jeesite.modules.purchaseoutbound.dao.PurchaseOutboundLinesDao;

/**
 * 采购退库单Service
 * @author 码脑洞
 * @version 2025-04-13
 */
@Service
public class PurchaseOutboundHeaderService extends CrudService<PurchaseOutboundHeaderDao, PurchaseOutboundHeader> {
	
	@Autowired
	private PurchaseOutboundLinesDao purchaseOutboundLinesDao;
	
	/**
	 * 获取单条数据
	 * @param purchaseOutboundHeader
	 * @return
	 */
	@Override
	public PurchaseOutboundHeader get(PurchaseOutboundHeader purchaseOutboundHeader) {
		PurchaseOutboundHeader entity = super.get(purchaseOutboundHeader);
		if (entity != null){
			PurchaseOutboundLines purchaseOutboundLines = new PurchaseOutboundLines(entity);
			purchaseOutboundLines.setStatus(PurchaseOutboundLines.STATUS_NORMAL);
			entity.setLinesList(purchaseOutboundLinesDao.findList(purchaseOutboundLines));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param purchaseOutboundHeader 查询条件
	 * @param purchaseOutboundHeader page 分页对象
	 * @return
	 */
	@Override
	public Page<PurchaseOutboundHeader> findPage(PurchaseOutboundHeader purchaseOutboundHeader) {
		return super.findPage(purchaseOutboundHeader);
	}
	
	/**
	 * 查询列表数据
	 * @param purchaseOutboundHeader
	 * @return
	 */
	@Override
	public List<PurchaseOutboundHeader> findList(PurchaseOutboundHeader purchaseOutboundHeader) {
		return super.findList(purchaseOutboundHeader);
	}
	
	/**
	 * 查询子表分页数据
	 * @param purchaseOutboundLines
	 * @param purchaseOutboundLines page 分页对象
	 * @return
	 */
	public Page<PurchaseOutboundLines> findSubPage(PurchaseOutboundLines purchaseOutboundLines) {
		Page<PurchaseOutboundLines> page = purchaseOutboundLines.getPage();
		page.setList(purchaseOutboundLinesDao.findList(purchaseOutboundLines));
		return page;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param purchaseOutboundHeader
	 */
	@Override
	@Transactional
	public void save(PurchaseOutboundHeader purchaseOutboundHeader) {
		super.save(purchaseOutboundHeader);
		// 保存 PurchaseOutboundHeader子表
		for (PurchaseOutboundLines purchaseOutboundLines : purchaseOutboundHeader.getLinesList()){
			if (!PurchaseOutboundLines.STATUS_DELETE.equals(purchaseOutboundLines.getStatus())){
				purchaseOutboundLines.setHeaderId(purchaseOutboundHeader);
				if (purchaseOutboundLines.getIsNewRecord()){
					purchaseOutboundLinesDao.insert(purchaseOutboundLines);
				}else{
					purchaseOutboundLinesDao.update(purchaseOutboundLines);
				}
			}else{
				purchaseOutboundLinesDao.delete(purchaseOutboundLines);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param purchaseOutboundHeader
	 */
	@Override
	@Transactional
	public void updateStatus(PurchaseOutboundHeader purchaseOutboundHeader) {
		super.updateStatus(purchaseOutboundHeader);
	}
	
	/**
	 * 删除数据
	 * @param purchaseOutboundHeader
	 */
	@Override
	@Transactional
	public void delete(PurchaseOutboundHeader purchaseOutboundHeader) {
		super.delete(purchaseOutboundHeader);
		PurchaseOutboundLines purchaseOutboundLines = new PurchaseOutboundLines();
		purchaseOutboundLines.setHeaderId(purchaseOutboundHeader);
		purchaseOutboundLinesDao.deleteByEntity(purchaseOutboundLines);
	}
	
}